package com.example.websocket.html;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HtmlTest {

    public static File input = new File("D:\\ideaProject\\websocket\\src\\main\\java\\com\\example\\websocket\\html\\new_goods.html");

    public static void test() {

        Document doc;

        {
            try {
                doc = Jsoup.parse(input, "UTF-8");
                Elements tbody = doc.body().getElementsByTag("table").first().getElementsByTag("tbody");
                List<GoodsPO> goodsPOS = new ArrayList<>();
                for (Element e : tbody){
                    Elements tr = e.getElementsByTag("tr");
                    for (Element er : tr){
                        Elements td = er.getElementsByTag("td");
                        List<String> text = new ArrayList<>();
                        for (Element ed : td){
                            text.add(ed.getElementsByTag("div").first().text());
                        }
                        if (text.get(13).equals("否") && (goodsPOS.isEmpty() ? true : goodsPOS.indexOf(text.get(0)) < 0)) {
                            goodsPOS.add(new GoodsPO(text));
                        }
                    }
                }
                System.out.println(goodsPOS.size());
                StringBuffer s = new StringBuffer();
                for (GoodsPO g : goodsPOS){
                    if (g.getTax().equals("-") || g.getTax() == null){
                        continue;
                    }
                    String tax = g.getTax();
//                    if (tax.indexOf("、") > 0){
//                        tax = tax.substring(tax.indexOf("、") + 1, tax.lastIndexOf("%"));
//                    } else {
                    tax = tax.substring(0, tax.indexOf("%"));
//                    }
//                    System.out.println(tax);
//                    String policy = g.getPolicy().equals("是") ? "1" : "0";
                    s.append("update tb_baiwang_taxcode set goods_tax = " + Double.valueOf(tax) / 100 + " where goods_code=" + g.getCode() + ";");
                }
                System.out.println(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        HtmlTest h = new HtmlTest();
        h.test();
    }

}
